<?php
include("general_include.php");
include "checklogin.php";
$page_name="inbox.php";
$IsPreserved	= 'Y';
$IsProcess		= $_REQUEST['IsProcess'];
$search			= $_REQUEST['search'];
$search_btn		=$_REQUEST['search_btn'];
$pmsg_id        = $_REQUEST['pmsg_id'];
if($pmsg_id!='')
 $smarty->assign('pmsg_id',$pmsg_id);

if($IsProcess!="Y")
	require_once('top.php');
else
	require_once("general_include.php");

$action=$_REQUEST['action'];
$user_id=$_SESSION['user_id'];
//$IsProcess	= 'Y';

	/* $latest_msg = "SELECT p . *, max(c . message_id )AS order_no  ,DATE_FORMAT(`p`.`date_added` ,'%D %M %Y %r' ) AS date_add , us.name AS c_user_id_from, us1.name AS p_user_id_from, us2.name AS c_user_id_to,c.message_text
					FROM ".TABLEPREFIX."_message p  
					LEFT JOIN ".TABLEPREFIX."_message c 
					ON p . message_id =c . reply_of_msg  
					LEFT JOIN gt_user us ON c.user_id_from = us.user_id
					LEFT JOIN gt_user us2 ON c.user_id_to = us2.user_id
					LEFT JOIN gt_user us1 ON p.user_id_from = us1.user_id					
					GROUP BY p . message_id   
					HAVING `p` . `reply_of_msg`=0 AND `p` . `user_id_to`=$user_id ".$where."
					ORDER BY order_no DESC,`p`.`message_id`  DESC";
	
	*/
	
	$where="";
	if($search_btn=='Search')
	{
		if(isset($search) || $search!='' )
		{
			$where=" AND ( c_user_id_to LIKE '%".$search."%' OR p_user_id_from LIKE '%".$search."%' OR c_user_id_from LIKE '%".$search."%' OR p.message_text LIKE '%".$search."%' OR c.message_text LIKE '%".$search."%') ";
		}		
	}
		
			
	$latest_msg = "SELECT p . *, max(c . message_id )AS order_no  ,DATE_FORMAT(`p`.`date_added` ,'%M  %d, %Y at %h %p' ) AS date_add 
					FROM ".TABLEPREFIX."_message p  LEFT JOIN ".TABLEPREFIX."_message c 
					ON p . message_id =c . reply_of_msg  
					GROUP BY p . message_id   
					HAVING `p` . `reply_of_msg`=0 AND `p` . `user_id_to`=$user_id
					ORDER BY `p`.`message_id`  DESC, order_no DESC";
					
	/*Pagicnation Start */
	$item_per_page_selected=6;
	$PaginationObjAjaxLatest=new PaginationClassAjax($item_per_page_selected,"prev",'',"next","active",$adodbcon);
	$pagination_arr		= $PaginationObjAjaxLatest->PaginationAjax($latest_msg,$page_name."?action=".$action,"ManagerGeneral");	
	/*Pagicnation End */
	
	
	$latest_msgArr = $UserManagerObjAjax->GetRecords("All",$pagination_arr[0]);
	//$latest_msgArr = $UserManagerObjAjax->GetRecords("All",$latest_msg);
	//print_r($latest_msgArr);
	
	for($i=0;$i<count($latest_msgArr);$i++)
	{
		 $userIsRead = "SELECT * FROM ".TABLEPREFIX."_message 
					WHERE is_read = 'N' AND user_id_from <> $user_id
					AND ( message_id =".$latest_msgArr[$i]['message_id']." OR reply_of_msg=".$latest_msgArr[$i]['message_id'].") ";
		$IsRead = $UserManagerObjAjax->GetRecords("All",$userIsRead);
		
		if(count($IsRead)==0)
		{
			 $latest_msgArr[$i]['read'] = 'Y';	
		}	
		else
		{
			 $latest_msgArr[$i]['read'] = 'N';
		}
		
		$userImg = "SELECT name,photo FROM ".TABLEPREFIX."_user WHERE user_id='".$latest_msgArr[$i]['user_id_from']."'";
		$ImgArr = $UserManagerObjAjax->GetRecords("Row",$userImg);
					
		$latest_msgArr[$i]['photo'] = $ImgArr['photo'];	
		$latest_msgArr[$i]['name'] = $ImgArr['name'];		
		
		$latest_msg_sub = "SELECT *,DATE_FORMAT(date_added ,'%M  %d, %Y at %h %p' ) AS date_add 
							FROM ".TABLEPREFIX."_message 
							WHERE reply_of_msg=".$latest_msgArr[$i]['message_id']."
							ORDER BY reply_of_msg,message_id  ";
		$latest_msg_subArr[$i] = $UserManagerObjAjax->GetRecords("All",$latest_msg_sub);
		if(count($latest_msg_subArr[$i])>0)
		{
			for($j=0;$j<count($latest_msg_subArr[$i]);$j++)
			{
				$userImg2 = "SELECT name,photo FROM ".TABLEPREFIX."_user WHERE user_id='".$latest_msg_subArr[$i][$j]['user_id_from']."'";
				$ImgArr2 = $UserManagerObjAjax->GetRecords("Row",$userImg2);
							
				$latest_msg_subArr[$i][$j]['photo'] = $ImgArr2['photo'];	
				$latest_msg_subArr[$i][$j]['name'] = $ImgArr2['name'];
			}
		}
	}
	
$smarty->assign('is_coach',$is_coach);
$smarty->assign('is_partner',$is_partner);
$smarty->assign('latest_msg_subArr',$latest_msg_subArr);		
$smarty->assign('latest_msgArr',$latest_msgArr);
$smarty->assign('latest_msgArrNum',count($latest_msgArr));
$smarty->assign('IsProcess',$IsProcess);
$smarty->assign('pagination_arr',$pagination_arr);
$smarty->assign('page_name',$page_name);
$smarty->display("message.tpl");
if($IsProcess!="Y")
	include("footer.php");
?>